package com.chen.base.config;

import cn.hutool.core.util.StrUtil;
import com.chen.base.utils.JoinPointUtil;
import com.chen.base.utils.ValidateUtil;
import com.chen.root.base.R;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author: 陈锦龙
 * @Date: 2024/8/19 10:20
 */
@Component
@Aspect
public class CheckParamAop {

    @Around("@annotation(com.chen.root.annotation.CheckParam)")
    public Object aroundMet(ProceedingJoinPoint point) throws Throwable {
        Map<String, Object> paramsMap = JoinPointUtil.paramsMap(point);
        StringBuilder builder = new StringBuilder();
        for (Object value : paramsMap.values()) {
            builder.append(ValidateUtil.validate(value));
        }
        if (StrUtil.isNotBlank(builder.toString())){
            return R.failed(builder.toString());
        }

        return point.proceed();
    }

}
